package com.shimao.celnet.payment.service.impl;

import com.shimao.celnet.payment.mapper.PInvoiceDaySumMapper;
import com.shimao.celnet.payment.model.PInvoiceDaySum;
import com.shimao.celnet.payment.model.request.CallOut_OutInvoiceSumRequest;
import com.shimao.celnet.payment.service.PInvoiceDaySumService;
import com.shimao.celnet.payment.util.MyDateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.shimao.celnet.payment.util.MyDateUtil.getPreDate;

@Service
public class PInvoiceDaySumServiceImpl extends BaseServiceImpl<PInvoiceDaySum>  implements PInvoiceDaySumService {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired(required =  false)
    private PInvoiceDaySumMapper pInvoiceDaySumMapper;
    /**
     * 查询出前一天00:00:00:--24:59:59的所有发票汇总数据
     * @return
     */
    @Override
    public List<PInvoiceDaySum> getByPage() {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(PInvoiceDaySum.class);
        Example.Criteria criteria = example.createCriteria();

        Date preDate = getPreDate();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String defaultStartDate = sdf.format(preDate);    //格式化前一天
        defaultStartDate = defaultStartDate+" 00:00:00";
        String defaultEndDate = defaultStartDate.substring(0,10)+" 23:59:59";
        //00:00:00:--24:59:59
        criteria.andBetween("invoiceTime",defaultStartDate,defaultEndDate);
        List<PInvoiceDaySum> tableList = super.getByExample(example);
        return  tableList;
    }
    /**
     * 支付日汇总
     * @return
     */
    @Override
    public List<CallOut_OutInvoiceSumRequest> selectSumInfo(String strDate) {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(PInvoiceDaySum.class);
        Example.Criteria criteria = example.createCriteria();
        Date preDate ;
        if(strDate == null || strDate == ""){
            preDate  = getPreDate();
        }else{
            preDate = MyDateUtil.StrToDate(strDate,"yyyy-MM-dd") ;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String defaultStartDate = sdf.format(preDate);    //格式化前一天
        Map<String,String> pMap = new HashMap<String,String>();
        //支付日期
        pMap.put("invoiceDate",defaultStartDate);

        List<CallOut_OutInvoiceSumRequest> tableList = pInvoiceDaySumMapper.selectSumInfo(pMap);

        return  tableList;
    }

    /**
     * 调用支付数据汇总存储过程
     */
    @Override
    public void activateProcess(String strDate){
        Date preDate ;
        if(strDate == null || strDate == ""){
            preDate  = getPreDate();
        }else{
            preDate = MyDateUtil.StrToDate(strDate,"yyyy-MM-dd") ;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String startDate = sdf.format(preDate);    //格式化前一天
        pInvoiceDaySumMapper.procInvoiceDaySum(startDate);
    }


}
